package wk.demo.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.luoye.bzijkplayer.R;
import com.suke.widget.SwitchButton;
import com.tencent.mmkv.MMKV;

import wk.demo.config.WkConfig;
import wk.demo.tool.MyDeviceTool;


public class SettingsFragment extends Fragment {

    public static SettingsFragment newInstance() {
        return new SettingsFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView =  inflater.inflate(R.layout.fragment_setting2, container, false);
        initView4(rootView);
        return rootView ;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private void initView4(View rootView){
        // 硬解码
        SwitchButton isWithHardDcodec = rootView.findViewById(R.id.isWithHardDcodec);
        boolean ischeck =  MMKV.defaultMMKV().decodeBool(WkConfig.KEY_WITH_HARDDECORDEC ,false );
        isWithHardDcodec.setChecked(ischeck);
        isWithHardDcodec.setOnCheckedChangeListener((view, isChecked) -> {
            MMKV.defaultMMKV().encode(WkConfig.KEY_WITH_HARDDECORDEC ,false) ;
        });

        // 后台播放
        SwitchButton enablePlayAtBakcground = rootView.findViewById(R.id.enablePlayAtBakcground) ;
        boolean isCheck = MMKV.defaultMMKV().decodeBool(WkConfig.KEY_PLAYATBACKGROUND ,false ) ;
        enablePlayAtBakcground.setChecked(isCheck);
        enablePlayAtBakcground.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                MMKV.defaultMMKV().encode(WkConfig.KEY_PLAYATBACKGROUND ,isChecked) ;
            }
        });

        // OpenSl es
        SwitchButton withOpensles = rootView.findViewById(R.id.withOpensles) ;
        boolean withSL = MMKV.defaultMMKV().decodeBool(WkConfig.KEY_WITH_OPENSLES ,false ) ;
        withOpensles.setChecked(withSL);
        withOpensles.setOnCheckedChangeListener((view, isChecked) -> MMKV.defaultMMKV().encode(WkConfig.KEY_WITH_OPENSLES ,isChecked));


        TextView appVersionTv = rootView.findViewById(R.id.appVersionTv);
        appVersionTv.setText(MyDeviceTool.getAppVersion(getActivity()));
    }


}
